//
//  DismissAnimator.swift
//  ReadHub
//
//  Created by testypp on 2017/12/22.
//  Copyright © 2017年 longyiqun. All rights reserved.
//

import Foundation
import UIKit

// Diss页面类 (自定义)
class DismissAnimator : NSObject {
}

extension DismissAnimator : UIViewControllerAnimatedTransitioning {
	func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
		return 0.6
	}
	
	func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
		guard
			let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
			let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
			else {
				return
		}
		
		transitionContext.containerView.insertSubview(toVC.view, belowSubview: fromVC.view)
		
		let screenBounds = UIScreen.main.bounds
		let bottomLeftCorner = CGPoint(x: 0, y: screenBounds.height)
		let finalFrame = CGRect(origin: bottomLeftCorner, size: screenBounds.size)
		
		UIView.animate(
			withDuration: transitionDuration(using: transitionContext),
			animations: {
				fromVC.view.frame = finalFrame
		},
			completion: { _ in
				transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
		}
		)
	}
}
